package com.cn.main.framework.proxy;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @aothor chenyuncong
 * @Date 2022/9/4 上午9:56
 */
public class RunFactoryBean<T> implements FactoryBean<T> {
    private Class<T> interfaceType;

    public Class<T> getInterfaceType() {
        return interfaceType;
    }

    public void setInterfaceType(Class<T> interfaceType) {
        this.interfaceType = interfaceType;
    }

    @Override
    public T getObject() throws Exception {
//        InvocationHandler handler = (proxy, method, args) -> {
//            System.out.println("代理T类型做一些事情");
//            return null;
//        };

        ProxyBean proxyBean = new ProxyBean();
        return (T) Proxy.newProxyInstance(interfaceType.getClassLoader(),
                new Class[]{interfaceType}, proxyBean);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceType;
    }
}
